﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using PackageActionsContrib;
using PackageActionsContrib.Helpers;


namespace CodeGecko.Umbraco.Modules.MSBuildPAC.Tasks
{
    public class UmbracoTaskBase : Task {
        private bool _undo { get; set; }
        [Required]
        public string Undo {
            get { return _undo.ToString(); }
            set { _undo = Convert.ToBoolean(value); }
        }
        public string PackageName { get; set; }
        public override bool Execute() {
            throw new NotImplementedException();
        }
    }

    //<Action runat="install" undo="true/false" alias="AddUrlRewriteRule">
    //  <add name="CWS_emaiAFriendID" virtualUrl="^~/email-a-friend/(.[0-9]*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/email-a-friend.aspx?nodeID=$1" ignoreCase="true" />
    //</Action>
    public class AddUrlRewriteRuleTask : UmbracoTaskBase
    {
        private string Alias { get { return "AddUrlRewriteRule"; } }
        [Required]
        public string Name { get; set; }
        [Required]
        public string VirtualUrl { get; set; }
        [Required]
        public string RewriteUrlParameter { get; set; }
        [Required]
        public string DestinationUrl { get; set; }
        [Required]
        public bool IgnoreCase { get; set; }
        private rdrMode redirectMode { get; set; }
        /*public string RedirectMode {
            get { return redirectMode.ToString(); }
            set { switch (value) {
                case "Permanent":
                    redirectMode = rdrMode.Permanent;
                    break;
                default:
                    throw new ArgumentException(value);
                    break;
                }
            }
        }*/
        public rdrMode RedirectMode { get; set; }
        public enum rdrMode { Permanent }
        public override bool Execute() {
            Log.LogMessage(MessageImportance.Low, "Adding URL rewriting rules to config/urlRewriting.config...");
            AddUrlRewriteRule action = new AddUrlRewriteRule();
            return action.Execute(
                (String.IsNullOrEmpty(this.PackageName) ? "MSBuildPAC" : this.PackageName),
                new XmlDocument());
        }
    }

    //<Action runat="install" undo="true/false" alias="AddAppConfigKey" key="your key" value="your value" />
    public class AddAppConfigKeyTask : UmbracoTaskBase {
        private string Alias { get { return "AddAppConfigKey"; } }
        [Required]
        public string Key { get; set; }
        [Required]
        public string Value { get; set; }
        public override bool Execute()
        {
            Log.LogMessage(MessageImportance.Low, "Adding <appSettings> key/value to web.config...");
            AddAppConfigKey action = new AddAppConfigKey();
            return action.Execute(
                (String.IsNullOrEmpty(this.PackageName) ? "MSBuildPAC" : this.PackageName),
                new XmlDocument());
        }
    }

    //<Action runat="install" undo="true/false" alias="AddLanguageFileKey" language="en/da/de/es/fr/it/nl/no/se/sv" position="beginning/end" area="template" key="demo" value="This is a demo string" />
    public class AddLanguageFileKeyTask : UmbracoTaskBase {
    }

    public class ExecutePackage : UmbracoTaskBase {
        //Find package.xml in project
        //Then, foreach <Action> element, create tasklist with various parameters.
        //
    }
}
